<?php

class Video extends DataObject
{
    // Declare data attributes
    protected $id;
    protected $title;
    protected $description = null;
    protected $userid = null;      
    protected $videotype;     
    protected $videolocation;
    protected $groupid;           
    protected $posttime;
    
    protected $group;
    protected $user;

    
    public function fetchFromDB($id, $id2=null)
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        // Name of stored procedure
        $proc = 'VideoFetch';
        $params = array($id);
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            $this->id = intval($id);
            $this->title = $returnData[0]["title"];
            $this->description = $returnData[0]["description"];
            $this->userid = intval($returnData[0]["userid"]);
            $this->videotype = intval($returnData[0]["videotype"]);
            $this->videolocation = $returnData[0]["videolocation"];
            $this->groupid = intval($returnData[0]["groupid"]);
            $this->posttime = $returnData[0]["posttime"];
            // Stored procedure succeeded, return true
            return true;
        }
        // Stored procedure failed, return false
        return false;
    }
    
    public function commit()
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        $proc = 'VideoCommit';
        $params = Array(
            $this->id,
            $this->title,
            $this->description,
            $this->userid,
            $this->videotype,
            $this->videolocation,
            $this->groupid,
            );
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function delete()
    {
        $db = DBConnection::inst(); 
        $proc = 'VideoDelete';
        $params = array($this->id);
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function create()
    {
        if (! isset($this->id))
        {
            // No database object stored.
            $db = DBConnection::inst();
            $proc = 'VideoCreate';
            $params = Array(
                $this->title,
                $this->description,
                $this->userid,
                $this->videotype,
                $this->videolocation,
                $this->groupid
            );
            
            // Sets the ID to the return value of the stored procedure, which
            // is the ID of the newly created objects.
            if ($this->id = $db->executeStoredProcedure($proc, $params, $returnData))
            {
                return true;
            }
            return false;
        }
        // data object already set
        return false;
    }
    
    public function link()
    {
        if (isset($this->group) && isset($this->user))
        {
            return true;
        }
        $DOM = DataObjectManager::inst();
        
        if (isset($this->groupid))
        {
            $this->group = $DOM->fetchDataObject("Group", $this->groupid);
            $this->group->link();
        }
        if (isset($this->userid))
        {
            $this->user = $DOM->fetchDataObject("Userid", $this->userid);
            $this->user->link();
        }
        
        return true;
    }
}

?>
